package morningClass;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class reflectPrc02 {
    @Test
    public void Cons() throws Exception {
        Class<?> s2 = Student.class;
        Constructor<?> constructor = s2.getConstructor(String.class, int.class);
        Object obj = constructor.newInstance("小明", 15);
        Student s=(Student) obj;
        System.out.println(s.age);
        System.out.println(s.name);
        s.study(10);
    }

    @Test
    public void getDF() throws Exception {
        Class<?> p = People.class;
        Constructor<?> declaredConstructor = p.getDeclaredConstructor(String.class, int.class);
        declaredConstructor.setAccessible(true);
        Object obj = declaredConstructor.newInstance("小明", 15);
        People p1=(People) obj;
        Field name = p.getDeclaredField("name");
        Field age = p.getDeclaredField("age");
        name.setAccessible(true);
        age.setAccessible(true);
        System.out.println(name.get(obj));
        System.out.println(age.get(obj));
        name.set(obj,"老王");
        age.set(obj,22);
        System.out.println(name.get(obj));
        System.out.println(age.get(obj));
        Method save = p.getDeclaredMethod("save", int.class, String.class);
        save.setAccessible(true);
        save.invoke(obj,10,"小李");

    }
}
